package com.fingard.dsp.bank.directbank.mbs01;

import com.alibaba.fastjson.JSONObject;
import com.epicc.mbs.utils.encrypt.UtilSM4;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IH5Order;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqUnifiedOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsUnifiedOrder;
import com.fingard.text.StringHelper;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MBS01H5Order extends MBS01Base implements IH5Order {
    @Override
    public RetAtsUnifiedOrder submitH5Order(ReqUnifiedOrder p_req) {
        RetAtsUnifiedOrder retAts = new RetAtsUnifiedOrder();
        retAts.rdSeq = p_req.rdSeq;
        try{
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_req.ownAct, "");
            if (actItem == null) {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息!";
                return retAts;
            }
            JSONObject reqmap = new JSONObject();
            reqmap.put("requestId", Format.DateTime14Format.format(new Date()));
            reqmap.put("outOrderID",p_req.rdSeq);
            reqmap.put("merchantID",actItem.merchantId);
            reqmap.put("openId",actItem.corpCode);
            reqmap.put("channel",p_req.channel);
            reqmap.put("title","保险");
            reqmap.put("notifyURL",actItem.notifyURL);
            reqmap.put("amount",StringHelper.amountMultiply100(p_req.amount));
            reqmap.put("timeStamp",Format.DateTime14Format.format(new Date()));
            reqmap.put("bankCode",p_req.bankCode);
            String cardNo = UtilSM4.encrypt_Ecb_Padding(actItem.appKey, p_req.oppAct.getBytes());
            WriteBankLogLn("加密结果16进制字符串："+cardNo);
            reqmap.put("cardNo",cardNo);
            reqmap.put("productDesc",p_req.description);
            reqmap.put("backParam",p_req.attach);
            reqmap.put("returnURL",actItem.returnURL+"?rdSeq="+p_req.rdSeq);
            String digest = getSignData(reqmap,actItem.saltKey);
            WriteBankLogLn("生成签名串："+digest);
            reqmap.put("sign",digest);
            String[] tmpStrRet = sendToBank(JSONObject.toJSONString(reqmap),bankFront.getUrl("")+"innerPay");
            if(tmpStrRet[0].length() == 0){
                HashMap<String, String> tmpRetMap = new HashMap<String, String>();
                tmpRetMap.put("Result", tmpStrRet[1]);

                retAts.respCode = "0";
                retAts.respInfo = "签名成功";
                retAts.customHashMap = tmpRetMap;
            }else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        }catch (Exception ex){
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return retAts;
    }

    @Override
    public RetAtsQueryOrder queryH5Pay(ReqQueryOrder p_req) {
        RetAtsQueryOrder retAts = new RetAtsQueryOrder();
        retAts.rdSeq = p_req.rdSeq;
        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_req.ownAct, "");
            if (actItem != null) {
                JSONObject reqmap = new JSONObject();
                reqmap.put("requestId", Format.DateTime14Format.format(new Date()));
                reqmap.put("outOrderID",p_req.rdSeq);
                reqmap.put("merchantID",actItem.merchantId);
                reqmap.put("openId",actItem.corpCode);
                reqmap.put("timeStamp",Format.DateTime14Format.format(new Date()));
                String digest = getSignData(reqmap,actItem.saltKey);
                WriteBankLogLn("生成签名串："+digest);
                reqmap.put("sign",digest);
                String[] tmpStrRet = sendToBank(JSONObject.toJSONString(reqmap),bankFront.getUrl("")+"queryOrder");
                if(tmpStrRet[0].length() == 0){
                    JSONObject result = JSONObject.parseObject(tmpStrRet[1]);
                    retAts.respInfo = result.get("rspMessage").toString();
                    retAts.transState = StringHelper.joinNotEmpty("+", result.get("rspCode").toString(), result.get("status").toString());
                    retAts.payInfoCode = result.get("rspCode").toString();
                    retAts.outTransID = result.get("outOrderID").toString();
                    retAts.respCode = "0";
                }else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                }
            } else {
                retAts.respCode = "-1";
                retAts.respCode = "未找到账号[" + p_req.ownAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return retAts;
    }
}
